#!/usr/bin/env python
#    SokobanV3 Main File
#

import soya,soya.pudding as pudding
import os,sys
from Sokoban import *


#class GameState:
#    QUIT=0
#    PLAY=1
#    MENU=2
#    pass
    
    
class Sokoban(soya.World):
    def __init__(self):
        
        soya.World.__init__(self)
        
        self.game=MainContext(parent=self)
        
        self.ctrl=Controller()
        #draw a simple object
        #Create game interface
        self.gameUI=GameUI(self)
        self.gameUI.menu_panel.next_level=self.game.next_level
        self.gameUI.menu_panel.create_menus()
        
        self.gameUI.menu_panel.next_level=self.game.next_level
        #self.current_widget=None
        self.camera = soya.Camera(self.game)

        self.camera.set_xyz(2.0,10.0,10.0)
        self.camera.look_at(self)
        
        
        
        #self.current_widget=self.camera1
    	self.gameUI.widget.add_child(self.camera)
    	soya.set_root_widget(self.gameUI.widget)

    	self.game_state=STATE_PLAY
        self.counter=0
        
    def change_game_state(self,state):
    	print "Game state changet to:",state
        #if self.game_state==state:return
        
        if state==STATE_MENU and self.game_state==STATE_MENU:
            self.gameUI.show(0)
            self.game_state=STATE_PLAY
        elif state==STATE_MENU and self.game_state==STATE_PLAY:
            self.gameUI.show(1)
            self.game_state=STATE_MENU
        else:
            self.game_state=state
            
    def advance_time(self,proportion):
        #print self.ctrl.tick()
        self.action_handler( (self.ctrl.tick()) )
        
        if self.game_state==STATE_PLAY:soya.World.advance_time(self.game,0.1*proportion)
            
        if self.game_state==STATE_QUIT:
            soya.quit()
            sys.exit("bye..")
        #self.game.level.advance_time(proportion)
        for child in self.children:
            child.advance_time(proportion)
    def action_handler(self,action):
        if not action:return
        action=action.action
        if action==TESTING:
            self.game.next_level()
        if action==GAME_QUIT:
            self.change_game_state(STATE_QUIT)
            return
        if action==GO_MENU:
            self.change_game_state(STATE_MENU)
            return
        
        if self.game_state==STATE_MENU:
            self.gameUI.action_handler(action)
            return
        if action==ACT_RIGHT:    self.camera.x+=0.5
        if action==ACT_LEFT:    self.camera.x-=0.5
        if action==ACT_FORWARD:    self.camera.z-=0.5
        if action==ACT_BACKWARD:    self.camera.z+=0.5
		
        
if __name__=="__main__":

    soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "game_data"))
    soya.init("Sokoban V3 attempt 1",width=800,height=500)
    pudding.init()
    
    game=Sokoban()
    print game.game.children
    pudding.main_loop.MainLoop(game).main_loop()
